import requests
import sqlite3
from lxml import etree
from itertools import repeat

category = [f"{j}"for j in range(1, 10)]

for i in range(1, 10):
    res = requests.get(f"https://www.xiaoshuopu.com/class_{i}/")
    content = res.text
    tree = etree.HTML(content)
    rec_data = tree.cssselect(".ultop li a")
    rec_datas = [rec.text for rec in rec_data]
    rec_datas = list(zip(rec_datas, repeat(category[i - 1])))
    print(rec_datas)

    con = sqlite3.connect("../mysite/db.sqlite3")
    cur = con.cursor()
    cur.executemany(f'insert into main_categorypagerecommend (name,category) values (?,?)', rec_datas)
    con.commit()
    cur.close()
    con.close()
